from django.db import models
from db.base_models import BaseModel
# Create your models here.


class GoodsStatus(BaseModel):
    """
    商品状态
    """
    status_name = models.CharField(max_length=10, verbose_name="状态名称")

    def __str__(self):
        return self.status_name

    class Meta:
        db_table = "goods_status"
        verbose_name = "商品状态"
        verbose_name_plural = verbose_name


class GoodsType(BaseModel):
    """
    商品分类
    """
    type_name = models.CharField(max_length=12, verbose_name="商品类型名称")
    zt_name = models.ForeignKey(GoodsStatus, on_delete=models.CASCADE, verbose_name="商品类型状态")

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = "goods_type"
        verbose_name = "商品分类"
        verbose_name_plural = verbose_name


class Unit(BaseModel):
    """
    单位
    """
    unit = models.CharField(max_length=10, verbose_name="单位")

    class Meta:
        db_table = "unit"
        verbose_name = "单位"
        verbose_name_plural = verbose_name


class Brand(BaseModel):
    """
    品牌
    """
    brand_name = models.CharField(max_length=10, verbose_name="品牌名称")

    class Meta:
        db_table = "brand"
        verbose_name = "品牌"
        verbose_name_plural = verbose_name


class VendorType(BaseModel):
    """
    供应商类型：自营，联营，租赁， 代销
    """
    type_name = models.CharField(max_length=10, verbose_name="供应商类型名称")
    zt_name = models.ForeignKey(GoodsStatus, on_delete=models.CASCADE, verbose_name="供应商类型状态")

    class Meta:
        db_table = "vendor_type"
        verbose_name = "供应商类型"
        verbose_name_plural = verbose_name


class Vendor(BaseModel):
    """
    供应商
    """
    vendor_code = models.CharField(max_length=10, verbose_name="供应商编号")
    vendor_name = models.CharField(max_length=30, verbose_name="供应商名称")
    vendor_type = models.ForeignKey(VendorType, on_delete=models.CASCADE, verbose_name="关联供应商类型")
    zt_name = models.ForeignKey(GoodsStatus, on_delete=models.CASCADE, verbose_name="供应商状态")

    class Meta:
        db_table = "vendor"
        verbose_name = "供应商"
        verbose_name_plural = verbose_name


class Goods(BaseModel):
    """
    商品
    """
    goods_type = models.ForeignKey(GoodsType, on_delete=models.CASCADE, verbose_name="商品分类")
    goods_code = models.CharField(max_length=18, verbose_name="商品编号")
    goods_name = models.CharField(max_length=30, verbose_name="商品名称")
    unit = models.ForeignKey(Unit, on_delete=models.DO_NOTHING, verbose_name="单位")
    spec = models.CharField(max_length=15, verbose_name="规格", blank=True, null=True)
    vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE, verbose_name="关联供应商")
    price = models.IntegerField(default=0, verbose_name="售价")
    j_price = models.IntegerField(default=0, verbose_name="进价")
    cx_price = models.IntegerField(default=0, verbose_name="限时售价")
    place = models.CharField(max_length=10, verbose_name="产地", blank=True, null=True)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, verbose_name="关联品牌", blank=True, null=True)
    goods_img = models.ImageField(upload_to="goods", verbose_name="商品图片", blank=True, null=True)
    zt_name = models.ForeignKey(GoodsStatus, on_delete=models.CASCADE, verbose_name="商品状态", blank=True, null=True)

    class Meta:
        db_table = "goods"
        verbose_name = "商品"
        verbose_name_plural = verbose_name


class GoodsPicture(BaseModel):
    """
    商品图片
    """
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="关联商品")
    price = models.ImageField(upload_to="goods", verbose_name="商品图片")

    class Meta:
        db_table = "goods_picture"
        verbose_name = "商品图片"
        verbose_name_plural = verbose_name


